﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using ERP_RORZE_SYSTEM;

namespace ERP_RORZE_DLL.BasicList
{
    public partial class frm_Add_BankAccount : Form
    {
        private INISettings mySetting;
        private SqlConnection myConnection;
        private string error;

        public frm_Add_BankAccount()
        {
            InitializeComponent();

            this.mySetting = new INISettings();
            this.myConnection = new SqlConnection(mySetting.SQLConnectionString);
            this.error = null;
            LoadCurrency();

            this.currencyID.KeyPress += currencyID_KeyPress;
        }

        void currencyID_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar == Convert.ToChar(Keys.Return))
            {
                this.buttonAdd.Focus();
            }
        }

        ////////////////////////////////////////////
        /// Load Currency List
        ////////////////////////////////////////////
        private void LoadCurrency()
        {
            try
            {
                myConnection.Open();
                SqlCommand cmd_sql = myConnection.CreateCommand();
                cmd_sql.CommandText = "SELECT CurrencyID FROM [222_Currency] ORDER BY CurrencyID";
                SqlDataReader dataReader = cmd_sql.ExecuteReader();
                while (dataReader.Read())
                {
                    ComboboxItem item = new ComboboxItem();
                    item.Text = dataReader["CurrencyID"].ToString();
                    item.Value = dataReader["CurrencyID"].ToString();
                    this.currencyID.Properties.Items.Add(item);
                }
                dataReader.Close();
                cmd_sql.Dispose();
                myConnection.Close();
            }
            catch
            {
                if (myConnection.State == ConnectionState.Open)
                {
                    myConnection.Close();
                }
                MessageBox.Show("Cannot get Currency list");
            }
        }

        private void buttonCancel_Click(object sender, EventArgs e)
        {
            this.Dispose();
        }

        private void buttonAdd_Click(object sender, EventArgs e)
        {
            error = null;
            string account_id = null;
            string account_number = null;
            string bank_name = null;            
            string currency_id = null;
            
            ComboboxItem item = new ComboboxItem();
            
            if (string.IsNullOrEmpty((this.accountID.Text)))
            {
                error = "Please enter Bank Account ID";
            }
            else
            {
                account_id = this.accountID.Text;
            }

            if (string.IsNullOrEmpty(error))
            {
                if (string.IsNullOrEmpty(this.accountNo.Text))
                {
                    error = "Please enter Bank Account No.";
                }
                else
                {
                    account_number = this.accountNo.Text;
                }
            }

            if (string.IsNullOrEmpty(error))
            {
                if (string.IsNullOrEmpty(this.bankName.Text))
                {
                    error = "Please enter Bank Name";
                }
                else
                {
                    bank_name = this.bankName.Text;
                }
            }

            if (string.IsNullOrEmpty(error))
            {
                if (this.currencyID.SelectedIndex < 0)
                {
                    error = "Please select Currency";
                }
                else
                {
                    item = (ComboboxItem)this.currencyID.SelectedItem;
                    currency_id = item.Value.ToString();
                }
            }

            if (string.IsNullOrEmpty(error))
            {
                try
                {
                    myConnection.Open();
                    string sql = "INSERT INTO [222_BankAccount] (BankAccountID, BankAccountNo, BankName, CurrencyID)"
                        + " VALUES ('" + account_id + "', '" + account_number + "', '" + bank_name + "', '" + currency_id + "')";
                    SqlCommand cmd_sql = new SqlCommand(sql, myConnection);
                    cmd_sql.ExecuteNonQuery();
                    cmd_sql.Dispose();
                    myConnection.Close();

                    clearForm();
                    MessageBox.Show("Bank Account added success.");
                }
                catch //(Exception ex)
                {
                    if (myConnection.State == ConnectionState.Open)
                    {
                        myConnection.Close();
                    }
                    //MessageBox.Show(ex.Message);
                    MessageBox.Show("Cannot add Bank Account. Please try again.");
                }
            }
            else
            {
                MessageBox.Show(error);
            }
        }

        private void clearForm()
        {
            foreach (Control c in this.Controls)
            {
                if (c is DevExpress.XtraEditors.TextEdit)
                {
                    ((DevExpress.XtraEditors.TextEdit)c).Text = null;
                }

                if (c is DevExpress.XtraEditors.ComboBoxEdit)
                {
                    ((DevExpress.XtraEditors.ComboBoxEdit)c).SelectedIndex = -1;
                }

                if (c is System.Windows.Forms.MaskedTextBox)
                {
                    ((System.Windows.Forms.MaskedTextBox)c).Text = null;
                }
            }            
        }
    }
}
